package com.feihong.txgw.adapter.Holder;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.TextView;

import com.feihong.txgw.R;
import com.feihong.txgw.adapter.ProductOrderDetailGoodsAdapter;
import com.feihong.txgw.http.HttpTools;
import com.feihong.txgw.model.base_bean.BaseResponse;
import com.feihong.txgw.ui.activity.order.PayActivity;
import com.feihong.txgw.ui.activity.order.ProductsOrderDetailsActivity;
import com.feihong.txgw.ui.activity.order.ReViewOrderActivity;
import com.feihong.txgw.ui.activity.product.ProductInfoActivity;
import com.feihong.txgw.ui.fragment.order.OrderListFragment;
import com.feihong.txgw.json.user.ProductOrderDetailEntity;
import com.feihong.txgw.message.RefreshOrder;
import com.feihong.txgw.utils.helper.RxHelper;
import com.feihong.txgw.utils.manage.RetrofitManager;
import com.feihong.txgw.view.NoScrollListView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.feihong.txgw.ui.fragment.home.DiscoverFragment.INTENT_PRODUCTID;

/**
 * @name txgw_app
 * @class name：com.feihong.txgw.adapter.Holder
 * @class describe:  终端订单详情
 * @author wangziheng
 * @time 2017/8/16 12:33
 * @change
 * @chang time
 * @class describe
 */
public class ProductOrderDetailGoodsViewHolder extends BaseViewHolder {
    private NoScrollListView mGoodsListView;
    private TextView mOrderDliveryName;
    private TextView mOrderDeliveryTel;
    private TextView mOrderDeliveryAdr;
    private TextView mOrderPayType;
    private TextView mOrderPayMoney;
    private TextView mCannelOrder;
    private TextView mPayOrder;
    private TextView mDeliveryPrice;
    private ProductOrderDetailGoodsAdapter mAdapter;
    private ProductsOrderDetailsActivity mContext;

    private String mOrderSn;
    private String orderID = "";
    private String mOrderGoodsId;
    private int type;

    private String orderStatus = "0";
    private OrderListFragment.OrderRefreshCallBack mRefreshCallBack;
    public ProductOrderDetailGoodsViewHolder(OrderListFragment.OrderRefreshCallBack refreshCallBack,View itemView, Context context) {
        super(itemView);
        mPayOrder = (TextView) itemView.findViewById(R.id.product_order_detail_quren);
        mCannelOrder = (TextView) itemView.findViewById(R.id.product_order_detail_cancle);
        mOrderPayMoney = (TextView) itemView.findViewById(R.id.product_order_detail_pay_money);
        mOrderPayType = (TextView) itemView.findViewById(R.id.product_order_detail_pay_way);
        mOrderDeliveryAdr = (TextView) itemView.findViewById(R.id.product_order_detail_get_address);
        mOrderDeliveryTel = (TextView) itemView.findViewById(R.id.product_order_detail_get_num);
        mOrderDliveryName = (TextView) itemView.findViewById(R.id.product_order_detail_get_person);
        mGoodsListView = (NoScrollListView) itemView.findViewById(R.id.productOrderDetail_productLv);
        mDeliveryPrice = (TextView) itemView.findViewById(R.id.product_order_detail_delivery_price);
        mContext = (ProductsOrderDetailsActivity) context;
        mAdapter = new ProductOrderDetailGoodsAdapter(mContext);
        mRefreshCallBack = refreshCallBack;
    }

    public void initData(final List<ProductOrderDetailEntity> mOrderGoods) {

        if (mOrderGoods != null && mOrderGoods.size() != 0) {
            orderID = mOrderGoods.get(0).getOrder_id();
//            if ("1".equals(mOrderGoods.get(0).getPay_type())) {
//                mOrderPayType.setText("微信小程序");
//            } else if ("2".equals(mOrderGoods.get(0).getPay_type())) {
//                mOrderPayType.setText("微信app");
//            } else if ("0".equals(mOrderGoods.get(0).getPay_type()) || TextUtils.isEmpty(mOrderGoods.get(0).getPay_type())) {
//                mOrderPayType.setText("未支付");
//            }
            if("1".equals(mOrderGoods.get(0).getSource())){
                 mOrderPayType.setText("PC端");
            }else if("2".equals(mOrderGoods.get(0).getSource())){
                mOrderPayType.setText("小程序端");
            }else if("3".equals(mOrderGoods.get(0).getSource())){
                mOrderPayType.setText("app端");

            }
            mDeliveryPrice.setText(mOrderGoods.get(0).getDelivery_price()+"元");
            mOrderPayMoney.setText(mOrderGoods.get(0).getSku_amount()+"元");
            mOrderDeliveryAdr.setText(mOrderGoods.get(0).getRegion_name()+mOrderGoods.get(0).getAddress_detail());
            mOrderDeliveryTel.setText(mOrderGoods.get(0).getPhonenum());
            mOrderDliveryName.setText(mOrderGoods.get(0).getAddress_name());
            orderStatus = mOrderGoods.get(0).getStatus();
            if ("1".equals(orderStatus)) {
                mCannelOrder.setText("取消订单");
                mPayOrder.setText("确认支付");
                mCannelOrder.setVisibility(View.VISIBLE);
                mPayOrder.setVisibility(View.VISIBLE);
            }else if("4".equals(orderStatus)){
                mCannelOrder.setVisibility(View.GONE);
                if(mOrderGoods.get(0).getComment_status() == 0){
                    mPayOrder.setText("评价");
                    mPayOrder.setVisibility(View.VISIBLE);
                }else {
                    mPayOrder.setVisibility(View.GONE);
                }
            }else if("3".equals(orderStatus)){
                mCannelOrder.setVisibility(View.GONE);
                mPayOrder.setText("确认收货");
                mPayOrder.setVisibility(View.VISIBLE);
            }else if("5".equals(orderStatus)){
                mCannelOrder.setVisibility(View.GONE);
                mPayOrder.setText("删除订单");
                mPayOrder.setVisibility(View.VISIBLE);
            }else if("2".equals(orderStatus)){

                mCannelOrder.setVisibility(View.GONE);
                mPayOrder.setText("等待收货");
                mPayOrder.setVisibility(View.VISIBLE);
            }
        }
        mGoodsListView.setAdapter(mAdapter);
        mAdapter.setGoods(mOrderGoods);

        mCannelOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mContext.showConfirmDialog("取消订单", "您确定要取消此订单信息吗？", "是", "否", new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                        RetrofitManager.getInstance().operateOrder(HttpTools.CANNEL_ORDER_URL_KEY,orderID,String.valueOf(PayActivity.PAY_TYPE_PRODUCT))
                                .compose(RxHelper.<BaseResponse>rxSchedulerHelper())
                                .subscribe(cannelOrderObserver);
                    }
                });
            }
        });
        mPayOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (orderStatus){
                    case "1":
                        Intent intent = new Intent(mContext, PayActivity.class);
                        intent.putExtra(PayActivity.INTENT_ORDER_ID, mOrderGoods.get(0).getOrder_sn());
                        intent.putExtra(PayActivity.INTENT_PRICE, mOrderGoods.get(0).getSku_amount());
                        intent.putExtra(PayActivity.INTENT_PAY_TYPE, PayActivity.PAY_TYPE_PRODUCT);
                        mContext.startActivity(intent);
                        break;
                    case "3":
                        mContext.showConfirmDialog("确认收货", "您确定要确认收货此订单吗？", "是", "否", new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                sweetAlertDialog.dismiss();
                                RetrofitManager.getInstance().operateOrder(HttpTools.CONFIRM_ORDER_URL_KEY,orderID,String.valueOf(PayActivity.PAY_TYPE_PRODUCT))
                                        .compose(RxHelper.<BaseResponse>rxSchedulerHelper())
                                        .subscribe(cannelOrderObserver);
                            }
                            });
                        break;
                    case "4":
                        Intent intenta = new Intent(mContext, ReViewOrderActivity.class);
                        intenta.putExtra(PayActivity.INTENT_ORDER_ID, mOrderGoods.get(0).getOrder_sn());
                        intenta.putExtra("order_id",mOrderGoods.get(0).getOrder_id());
                        intenta.putExtra(PayActivity.INTENT_PAY_TYPE, PayActivity.PAY_TYPE_PRODUCT);
                        intenta.putExtra("trueRefresh",1);
                        mContext.startActivity(intenta);
                        break;
                    case "5":
                        mContext.showConfirmDialog("删除订单", "您确定要删除此订单信息吗？", "删除", "取消", new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                sweetAlertDialog.dismiss();
                                RetrofitManager.getInstance().operateOrder(HttpTools.DEL_ORDER_URL_KEY,orderID,String.valueOf(PayActivity.PAY_TYPE_PRODUCT))
                                        .compose(RxHelper.<BaseResponse>rxSchedulerHelper())
                                        .subscribe(cannelOrderObserver);
                            }
                        });
                        break;
                    default:break;
                }

            }
        });

        mGoodsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Adapter adapter = adapterView.getAdapter();
                ProductOrderDetailEntity productOrderDetailEntity = (ProductOrderDetailEntity) adapter.getItem(i);
                Intent intent = new Intent();
                intent.setClass(mContext, ProductInfoActivity.class);
                intent.putExtra(INTENT_PRODUCTID, productOrderDetailEntity.getSku_id());
                mContext.startActivity(intent);
            }
        });



    }

    Observer<BaseResponse> cannelOrderObserver = new Observer<BaseResponse>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(BaseResponse baseResponse) {
            if (baseResponse.code == 200) {
                EventBus.getDefault().post(new RefreshOrder());
                mContext.showDialog("", baseResponse.msg, new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        dialog.dismiss();
                        RefreshOrder refreshOrder = new RefreshOrder();
                        refreshOrder.setOrderStatus(Integer.valueOf(orderStatus));
                        EventBus.getDefault().post(new RefreshOrder());
                        mRefreshCallBack.refresh(0);
                        mContext.finish();
                    }
                });

            } else {
                mContext.showDialog("", baseResponse.msg);
            }
        }

        @Override
        public void onError(Throwable e) {
            mContext.showDialog(null, "网络错误，提交失败！");
        }

        @Override
        public void onComplete() {

        }
    };

}
